#import "WeatherResponse.h"
#import "ProvinceAndCity.h"
#import "RequestCreator.h"
@implementation WeatherResponse
@synthesize days;
@synthesize cityName;
@synthesize provinceName;

@synthesize updateTime, humi, temp, wind, mainImage;
-(WeatherResponse *) initWithListener: (WeatherViewController *) _controller provinceName: (NSString *) _provinceName cityName: (NSString *) _cityName{
	if (self = [super init]) {
		cityName = _cityName;
		provinceName = _provinceName;
		controller = _controller;
	}
	return self;
}

-(void) load{
	responseData = [[NSMutableData alloc] init];
	days = [[NSMutableArray alloc] init];
	[controller.indicator startAnimating];
	[RequestCreator invokeWs: RequestGetWeather envelope: [RequestCreator createGetWeather: cityName] delegate: self];
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
	[responseData appendData: data];
}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"error" message: [error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
	[alert show];
	[alert release];
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
	NSXMLParser *parser = [[NSXMLParser alloc] initWithData: responseData];
	[parser setDelegate: self];
	[parser parse];	
}


#pragma mark parse xml
-(void) parserDidStartDocument:(NSXMLParser *)parser{
	[days removeAllObjects]; 
	valueIndex = 0;
}

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
	[containerString release];
	containerString = [[NSMutableString alloc] init];
	if (valueIndex == 7) {
		day = [[Day alloc] init];
	}
}

-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
	if ([elementName isEqualToString: @"getWeatherResult"]) {
		mainImage = containerString;
	}
	
	NSLog(@"%d %@", valueIndex, containerString);
	if (valueIndex >= 7) {
		NSUInteger partIndex = (valueIndex - 7) % 5;
		switch (partIndex) {
			case 0:
				day.which = containerString;
				break;
			case 1:
				day.temp = containerString;
				break;
			case 2:
				day.wind = containerString;
				break;
			case 3:
				day.image = containerString;
				if (day.which) {
					[days addObject: day];
				}				
				[day release];
				day = [[Day alloc] init];
				break;
			default:
				break;
		}
		
	}else {
		if (valueIndex == 3) {
			updateTime = [containerString retain] ;
		}
		if (valueIndex == 4) {
			//今日天气实况：气温：6.3℃；风向/风力：东北风 小于3级；湿度：91%；气压：1024.3hPa
			NSArray *array = [containerString componentsSeparatedByString: @"；"];
			temp = [[[array objectAtIndex: 0] componentsSeparatedByString: @"况："] objectAtIndex: 1];
			wind = [array objectAtIndex:1];
			humi = [array objectAtIndex:2];
		}
	}
	
	valueIndex ++;
}

-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
	[containerString appendString: string];
}

-(void) parserDidEndDocument:(NSXMLParser *)parser{
	[responseData release];
	[controller reloadWeather];
}

@end
